// pagesDoctor/prescribing/index.js
import {
  ApisubmitDoctorAdvice,
  ApiGetOrderInfo,
} from '../../api/docIndex'
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    opt: {
      ICD: '',
      ICD1: '',
      type: '',
      disease: '',
      disease_code: '',
      main: '',
      medical: [],
    },
  },
  /*   adviceArr={"id":2,"order_id":117,"uid":1,"ICD":"霍乱","ICD1":"霍乱","type":"霍乱","disease":"霍乱","main":"11321321222","medical":[{"id":4,"type":"非处方药","pic":"files/admin/20241217/20241217133448_感冒清.jpg","name":"维塔利胶囊","common_name":"维塔利胶囊","price":"128.00","introduce":"维塔利胶囊维塔利胶囊维塔利胶囊","size":"15/次","ingredient":"甲钴胺","feature":"--","save":"--","note":"--","guide":"--","created_at":"2024-12-17 13:35:17","updated_at":"2024-12-17 13:35:17","checkStep":false,"count":"1","doseFrequency":"2","days":"2","precautions":"2","num":"2"}],"use":0,"created_at":"2025-03-04 16:21:45","updated_at":"2025-03-04 16:25:57","patient_id":5,"first_review_uid":5,"first_review_time":"2025-03-04 16:25:57","first_review_result":2,"second_review_uid":null,"second_review_time":null,"second_review_result":null,"not_pass_reason":"21321322222","invalid":0,"doctor_id":2,"patient_name":"李四","patient_gender":"男","status":"审核未通过","first_review_doctor":"白居易","second_review_doctor":""} */

  //开药
  openClick(e) {
    let value = e.currentTarget.dataset.v
    if (value == '开药') {
      if (!this.data.opt.main) {
        wx.showToast({
          title: '请输入主诉',
          icon: "error",
        })
        return
      }
      if (!this.data.opt.disease) {
        wx.showToast({
          title: '请选择疾病',
          icon: "error",
        })
        return
      }
      wx.navigateTo({
        url: `/pagesDoctor/prescribeMedicineData/index?disease=${this.data.opt.disease}&&main=${this.data.opt.main}&&medical=${JSON.stringify(this.data.opt.medical)}&&isReview=${this.data.isReview}&disease_code=${this.data.opt.disease_code}`,
      })
    }
    if (value == '病种选择') {
      this.setData({
        isReview:false
      })
      wx.navigateTo({
        url: '/pagesDoctor/diseaseSelection/index',
      })
    }
  },

  
  //一键导入
  getChangeData(e) {
    let val = e.detail.value
    val.medical = JSON.parse(val.medical)
    this.setData({
      opt: val,
      isReview: false,
    })
  },
  //导入处方模板
  importTemplate() {
    this.setData({
      showpopup: true
    })
  },





  /*  sendPrescriptions(val, consultation_order_id, doctor_id) {
     const parmas = {
       consultation_order_id: consultation_order_id,
       action: "message",
       receiver: doctor_id,
       content: val,
       type: "prescription",
       sender: wx.getStorageSync('userInfo').id,
       sender_type: "doctor",
       id: Date.now()
     }
     app.ws.send(parmas)

   }, */
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      orderId: options.orderId,
      opt: options.adviceArr ? JSON.parse(options.adviceArr) : this.data.opt,
      isReview: options.adviceArr ? true : false,
    })
  },

  //导入主要诉讼信息
  importData() {
    ApiGetOrderInfo(this.data.orderId).then(res => {
      this.setData({
        'opt.main': res.data.patient_status
      })
    })
  },

  onChange(e) {
    console.log(e)
    this.setData({
      'opt.main': e.detail
    })
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    //识别所需要填写的数据
    let pages = getCurrentPages();
    let currPage = pages[pages.length - 1];
    console.log(currPage.data,this.data)
    if (currPage.data && !this.data.isReview) {
      let main = this.data.opt.main
      for (const key in this.data.opt) {
        this.data.opt[key] = currPage.data[key]
        this.data.opt.main = main
      }
      this.setData({
        opt: this.data.opt,
      })
    }
  }

  /*   console.log(this.data.opt) */
  /*  if (currPage.data.ICD) {
     this.setData({
       'ICD': currPage.data.ICD
     })
   } */



})